package com.bc.upgrade.utils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * 思路: 通过传递的oldApk和newApk生成差异包,并返回一个Url地址
 * 
 * @author duguang
 * @date 2013.12.29
 * @version 1.0
 * @boke http://blog.csdn.net/duguang77
 * 
 */
public class BsdiffUtil {

	/**
	 * 通过传递的oldApk和newApk生成差异包,并返回一个Url地址
	 * 
	 * @param oldApk
	 *            客户端上的旧版本号
	 * @param newApk
	 *            服务器上最新的版本号
	 * @return
	 */
	public static String getPatchUrl(String oldApk, String newApk) {
		
		//bsdiff工具地址,放在自己服务器对应的地址下就行了
		String bsdiff = "D:/apache-tomcat-7.0.40/webapps/UpApk/upload/bsdiff";
		
		//旧apk的地址位置
		String oldFilePath = "D:/apache-tomcat-7.0.40/webapps/UpApk/upload/"
				+ oldApk + ".apk";
		
		//新apk的地址位置
		String newFilePath = "D:/apache-tomcat-7.0.40/webapps/UpApk/upload/"
				+ newApk + ".apk";
		
		//生成差异包地址位置
		String patchFilePath = "D:/apache-tomcat-7.0.40/webapps/UpApk/upload/"
				+ oldApk + "-" + newApk+".patch";
		
		String url = null;
		try {
			url = "http://10.0.2.2:8080/UpApk/upload/" + oldApk
					+ "-" + newApk + ".patch";
			File file = new File(url);
			System.out.println(url);
			if(file.exists()){
				return url;
			}else{
				Runtime rt = Runtime.getRuntime();

				rt.exec(bsdiff + " " + oldFilePath + " " + newFilePath + " "
						+ patchFilePath);
			}
			
			// rt.exec("d:/bsdiff d:/old.apk d:/new.apk d:/patch.patch");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return url;
	}

}
